package Action;

import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import Beans.Admin;
import Beans.Courier;
import Beans.LeftPackage;
import Beans.LogParcel;
import Beans.Package;
import Beans.Terminal;
import Beans.User;
import DAO.BoxDao;
import DAO.CourierDao;
import DAO.LogParcelDao;
import DAO.PackageDao;
import DAO.TerminalDao;
import DAO.UserDao;

import Utils.MakeExcelHelper;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class PackageAction extends ActionSupport {

	private int terminalId;
	private int branch;
	private int packageID;
	private int courierID;
	private String companyName;
	private String phoneNum;
	private String returnType;
	private String parcelID;
	private String checkCode;
	private String beginTime;
	private String endTime;
	private String message;
	private String funId;
	private String rfid;
	private int status;
	private LogParcel logParcel = null;
	private Package p = null;
	private Courier courier = null;
	private User user = null;
	private Timestamp starttime;
	private Timestamp endtime;
	
	static List<Package> packages;
	List<Terminal> terminals;
	List<LeftPackage> leftPackages;
	List<LogParcel> logParcels;
	List<User> users;

	PackageDao packageDao = new PackageDao();
	TerminalDao terminalDao = new TerminalDao();
	BoxDao boxDao = new BoxDao();
	LogParcelDao logParcelDao = new LogParcelDao();
	CourierDao courierDao = new CourierDao();
	UserDao userDao = new UserDao();
	
	public String terminalPackageStatus() {

		packages = packageDao.PackagesInThisTerminal(terminalId);

		if (packages != null) {
			return "terminalPackageStatus";
		} else {
			return "false";
		}

	}
	public String returnJSP() {
		return "jsp";
	}
	/**
	 * 修改包裹状态
	 */
	public void editPackage() {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		Admin admin = (Admin) session.get("admin");
		packageDao.editPackage(packageID,status);
		if(status==3)message="联系不上";
		if(status==4)message="退件";
		if(status==7)message="空包";
		if(status==8)message="错分件";
		logParcel = new LogParcel(packageDao.getPackageDetailsByID(packageID), new Timestamp(new Date().getTime()), admin, "设置包裹状态为【"+message+"】", "");
		logParcelDao.save(logParcel);
	}

	/**
	 * 今日包裹信息
	 * 
	 * @return 快递包裹的集合
	 */
	public String getPackagesToday() {

		packages = packageDao.getPackagesToday();
		if (returnType == null) {

			return "success";
		} else if (returnType.equals("json")) {

			return "jsonSuccess";
		}
		return "success";

	}

	/**
	 * 根据电话号码返回查询结果 如果请求串带有returnType=json返回JSON格式的结果
	 * 
	 * @return 快递包裹的集合
	 */
	public String packageDetailByPhoneNum() {

		packages = packageDao.getPackageDetailByPhone(phoneNum);
		if (returnType == null) {

			return "success";
		} else if (returnType.equals("json")) {

			return "jsonSuccess";
		}
		return "success";
	}
	
	/**
	 * 通过快递员手机查询包裹
	 * @return
	 */
	public String getByCourierPhone() {
		packages = packageDao.getByCourierPhone(phoneNum);
		return "success";
	}
	
	public String getByAgent() {
		terminals = terminalDao.returnTermialByBranchID(branch);
		packages = packageDao.getByAgent(terminals,starttime,endtime);
		return "success";
	}
	/**
	 * 获取未取包裹Excel
	 * @throws Exception
	 */
	public void getExcel() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
//		packages = (List<Package>) packageDao.getUnTakenPackage(terminalID);
//		Timestamp timestamp = new Timestamp(new Date().getTime());
//		for (Package p : packages) {
//			p.leftTime = (timestamp.getTime() - p.getStoreTime().getTime())
//					/ (1000 * 60 * 60 * 24) + "";
//		}
		MakeExcelHelper.MakeExcelByPackage(packages, response);
	}
	/**
	 * 通过快递公司查询包裹
	 * @return
	 */
	public String getByCompany() {
		packages = packageDao.getByCompany(companyName);
		return "success";
	}
	/**
	 * 通过快递员rfid查询包裹
	 * @return
	 */
	public String getByCourierRfid() {
		packages = packageDao.getByCourierRfid(rfid);
		return "success";
	}

	/**
	 * 获取退件列表
	 * @return
	 */
	public String returnPackage() {
		packages = packageDao.getReturnPackages();
		return "success";
	}
	/**
	 * 获取包裹日志表
	 * @return
	 */
	public String getLogParcel() {
		p = packageDao.getPackageDetailsByID(packageID);
		int flag = p.getFlag();
		int status = p.getPackageStatus();
		users = userDao.getAllUserPhoneNum(p.getUserPhone());
		if(users.size()==1){
			user = users.get(0);
		}
		if(flag==0&&status==0){
			p.setFlag(1);
			packageDao.editPackage(p);
			logParcel = new LogParcel(p, p.getStoreTime(), p.getCourier(), "快递员【存入包裹】", "");
			logParcelDao.save(logParcel);
		}
		if(flag==1&&status==1){
			p.setFlag(2);
			packageDao.editPackage(p);
			logParcel = new LogParcel(p, p.getGetTime(), user, "用户【取出包裹】", "");
			logParcelDao.save(logParcel);
		}
		if(flag==0&&status==1){
			p.setFlag(2);
			packageDao.editPackage(p);
			logParcel = new LogParcel(p, p.getStoreTime(), p.getCourier(), "快递员【存入包裹】", "");
			logParcelDao.save(logParcel);
			logParcel = new LogParcel(p, p.getGetTime(), user, "用户【取出包裹】", "");
			logParcelDao.save(logParcel);
		}
		logParcels = logParcelDao.getByParcelID(packageID);
		return "logParcel";
	}

	/**
	 * 处理退件
	 * @return
	 */
	public String dealedReturnPackage() {
		packages = packageDao.getDealedPackages();
		return "success";
	}
	/**
	 * 获取快递员存件情况
	 * @return
	 */
	public String getCourierStore() {
		packages = packageDao.getCourierStore(courierID);
		return "success";
	}

	/**
	 * 根据快递单号返回快递包裹的集合
	 * 
	 * @return
	 */
	public String packageDetailByParcelID() {

		packages = packageDao.getPackageDetailByParcelID(parcelID);

		if (returnType == null) {

			return "success";
		} else if (returnType.equals("json")) {

			return "jsonSuccess";
		}
		return "success";
	}

	/**
	 * 统计指定时间段内终端的使用率
	 * 
	 * @return
	 * @throws ParseException
	 */
	public String TerminalUseBetweenPeriod() throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		Date beginDate = sdf.parse(beginTime);
		Date endDate = sdf.parse(endTime);
		terminals = terminalDao.returnAllTermials();
		packages = packageDao.getTerminalUseBetweenPeriod(beginDate, endDate);
		int temp;
		for (Terminal t : terminals) {
			temp = 0;
			for (Package p : packages) {
				if (p.getTerminal().getTerminalId() == t.getTerminalId()
						&& p.getTerminal().getBranch().getBranchId() == t
								.getBranch().getBranchId()) {
					temp++;
				}
			}
			t.count = temp;
			t.boxes = boxDao.returnBoxesInThisTerminal(t.getTerminalId());
		}
		return "success";
	}

	/**
	 * 查询指定时间段内存入终端的未取包裹
	 * 
	 * @return
	 * @throws ParseException
	 */
	public String UnTakenPakcagesBetweenPeriod() throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		Date beginDate = sdf.parse(beginTime);
		Date endDate = sdf.parse(endTime);
		packages = packageDao.getUnTakenPackagesDetailBetweenPeriod(terminalId,
				beginDate, endDate);
		return "success";
	}
	
	/**
	 * 放回用户手机号码
	 * @return
	 * @throws Exception
	 * by zgy
	 */
	public String getUserPhoneByParcelID() throws Exception {
		Package p = packageDao.getPackageDetailsByID(packageID);
		if(p==null) {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.print("查询不到原手机号码。");
			out.close();
		} else {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.print(p.getUserPhone());
			out.close();
		}
		return null;
	}

	/**
	 * 滞留包裹统计结果
	 * 
	 * @return
	 * @throws ParseException
	 */
	public String leftPackageConstResult() throws ParseException {
		leftPackages = packageDao.PackagesInThisBranch(branch);
		return "success";
	}

	public String getReturnType() {
		return returnType;
	}

	public void setReturnType(String returnType) {
		this.returnType = returnType;
	}

	public List<Package> getPackages() {
		return packages;
	}

	public void setPackages(List<Package> packages) {
		PackageAction.packages = packages;
	}

	public String getPhoneNum() {
		return phoneNum;
	}

	public void setPhoneNum(String phoneNum) {
		this.phoneNum = phoneNum;
	}

	public int getTerminalId() {

		return terminalId;
	}

	public void setTerminalId(int terminalId) {

		this.terminalId = terminalId;
	}

	public String getParcelID() {
		return parcelID;
	}

	public void setParcelID(String parcelID) {
		this.parcelID = parcelID;
	}

	public String getCheckCode() {
		return checkCode;
	}

	public void setCheckCode(String checkCode) {
		this.checkCode = checkCode;
	}

	public String getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public List<Terminal> getTerminals() {
		return terminals;
	}

	public void setTerminals(List<Terminal> terminals) {
		this.terminals = terminals;
	}

	public int getBranch() {
		return branch;
	}

	public void setBranch(int branch) {
		this.branch = branch;
	}

	public List<LeftPackage> getLeftPackages() {
		return leftPackages;
	}

	public void setLeftPackages(List<LeftPackage> leftPackages) {
		this.leftPackages = leftPackages;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getFunId() {
		return funId;
	}

	public void setFunId(String funId) {
		this.funId = funId;
	}

	public int getPackageID() {
		return packageID;
	}

	public void setPackageID(int packageID) {
		this.packageID = packageID;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}
	public List<LogParcel> getLogParcels() {
		return logParcels;
	}
	public void setLogParcels(List<LogParcel> logParcels) {
		this.logParcels = logParcels;
	}
	public String getRfid() {
		return rfid;
	}
	public void setRfid(String rfid) {
		this.rfid = rfid;
	}
	public void setLogParcel(LogParcel logParcel) {
		this.logParcel = logParcel;
	}
	public Package getP() {
		return p;
	}
	public void setP(Package p) {
		this.p = p;
	}
	public Courier getCourier() {
		return courier;
	}
	public void setCourier(Courier courier) {
		this.courier = courier;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public List<User> getUsers() {
		return users;
	}
	public void setUsers(List<User> users) {
		this.users = users;
	}
	public String getCompanyName() {
		return companyName;
	}
	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}
	public int getCourierID() {
		return courierID;
	}
	public void setCourierID(int courierID) {
		this.courierID = courierID;
	}
	public Timestamp getStarttime() {
		return starttime;
	}
	public void setStarttime(Timestamp starttime) {
		this.starttime = starttime;
	}
	public Timestamp getEndtime() {
		return endtime;
	}
	public void setEndtime(Timestamp endtime) {
		this.endtime = endtime;
	}
	
}